package com.zoweunion.mechanic.plugins.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Description:操作redis的工具类
 */
@SuppressWarnings("unchecked")
@Component
public class RedisUtil {
 
	
    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;
 
    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * 写入缓存  指定时间  单位：秒
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    
    /**
     * 添加hash属性
     * @param key
     * @param field
     * @param value
     * @return
     */
	public boolean hset(String key, String field, String value) {
		boolean result = false;
		try {
			redisTemplate.opsForHash().put(key,field,value);
			result=false;
		} catch (Exception e) {
		}
		return result;
	}
    
 
    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
 
    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
    	Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }
 
    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }
 
    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0){
            redisTemplate.delete(keys);
        }
    }
 
    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 读取list格式数据
     */
    public List<Map<String, Object>> getList(String keys) {

        return redisTemplate.opsForList().range(keys, 0, -1);
    }


    public void deleteList(String keys,Map<String, Object> values) {
        redisTemplate.opsForList().remove(keys,0,values);
    }

    public long setRemove(String key, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }



    /**
     * 读取list格式数据
     */
    public List<String> getListForString(String keys) {

        return redisTemplate.opsForList().range(keys, 0, -1);
    }

    /**
     * 读取list格式数据
     */
    public List<String> getListString(String keys) {

        return redisTemplate.opsForList().range(keys, 0, -1);
    }

    /**
     * 移除list某条数据
     */
    public void removeList(String keys,String value) {
        redisTemplate.opsForList().remove(keys,0,value);
    }

    /**
     * 插入list格式数据
     */
    public void insertList(List<Map<String, Object>> list,String key) {
        redisTemplate.opsForList().rightPushAll(key,list);
    }

    public boolean lSet(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 插入list格式数据
     */
    public void insertListObject(Object list,String key) {
        redisTemplate.opsForList().rightPushAll(key,list);
    }

    /**
     * 插入list格式数据
     */
    public void insertListForMap(Map<String, Object> map,String key) {
        redisTemplate.opsForList().rightPushAll(key,map);
    }

    /**
     * 插入list格式数据
     */
    public void insertListForString(String str,String key) {
        redisTemplate.opsForList().rightPushAll(key,str);
    }

    /**
     * 插入set格式数据
     */
    public void insertSetForString(String str,String key) {
        redisTemplate.opsForSet().add(key,str);
    }


    /**
     * 插入list格式数据
     */
    public void updateListForMap(String key,int index,Map<String, Object> map) {
        redisTemplate.opsForList().set(key,index,map);
    }

    /**
     * 插入list格式数据
     */
    public void removeListForMap(String key,int index,Map<String, Object> map) {

        redisTemplate.opsForList().remove(key,index,map);
    }
}